package club.mzwh.security.service.impl;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.security.core.Authentication;

public class LogoutSuccessHandler implements org.springframework.security.web.authentication.logout.LogoutSuccessHandler {

	private static final String CLIENT_ID = "TestSSO";
	private static final String REDIRECT_URI = "http://localhost:8088/sso/login";
	private static final String RESPONSE_TYPE = "code";
	
//	@Autowired
//	private ActionLogService actionService;
//	
//	@Autowired
//	private LogCategoryService logCategoryService;
	
	@Override
	public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
			throws IOException, ServletException {
		if (authentication != null) {
			/*****
			 * 退出登录Log
			 */
			try {
//				UserDetailModel user = (UserDetailModel)authentication.getPrincipal();
//				Date date = new Date();
//				ActionLogModel logModel = new ActionLogModel();
//				logModel.setUsername(user.getName());
//				logModel.setUserID(user.getUsername());
//				logModel.setWebsiteNameString(user.getCurrentSite());
//				logModel.setUserIP(RequestUtil.getIP(request));
//				logModel.setActionClass("登录管理");
//				logModel.setActionMethod("退出");
//				LogCategoryModel model = logCategoryService.getModelByClassAndMethod("loginOut", "loginOut");
//				logModel.setLogCategoryModel(model);
//				logModel.setCreateTime(date);
//				actionService.save(logModel);
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		HttpSession session = request.getSession();
		// 获取session中所有的键值  
		System.out.println("--------登出----------:"+session.isNew());
		Enumeration<?> enumeration = session.getAttributeNames();
		// 遍历enumeration
		while (enumeration.hasMoreElements()) {
		  // 获取session的属性名称
		  String name = enumeration.nextElement().toString();
		  // 根据键值取session中的值
		  Object value = session.getAttribute(name);
		  // 打印结果
		  System.out.println("name:" + name + ",value:" + value );
		}
		System.out.println("Call logout");
		request.setAttribute("url", "http://localhost:8080/oauth/authorize?client_id="+CLIENT_ID+"&redirect_uri="+REDIRECT_URI+"&response_type=code");
		request.setAttribute("name", "梅竹文化");
		request.getRequestDispatcher("/view/login.jsp").forward(request, response);
	}

}
